import Taro from '@tarojs/taro-rn';
import * as tslib_1 from "tslib";
import React from 'react';
import { Component } from "@tarojs/taro-rn";
import { View, Image, Text, Textarea } from "@tarojs/components-rn";
import { inject, observer } from "@tarojs/mobx-rn";
import { getRuntime } from "../../utils/common";
import order_confirm_customStyleSheet from "./order_confirm_custom_styles";
import img_09 from "../../assets/img/img_09.jpg";
import right_white from "../../assets/img/right_white.png";
import address from "../../assets/img/icon_58.png";
var _styleSheet = order_confirm_customStyleSheet;
let Personnel = class Personnel extends Component {
  constructor(props) {
    super(props);
    this.state = {
      defaultAddress: null,
      itemId: null,
      item: {},
      message: ''
    };
  }
  componentWillMount() {
    Taro.setNavigationBarTitle({ title: '确认订单' });
    let itemId = this.$router.params.id;
    let userCart = getRuntime().getStorage('userCart');
    console.log(userCart);
    if (itemId && userCart) {
      this.setState({ item: userCart });
      /*userCart.map(item => {
        if (parseInt(itemId) === parseInt(item.itemId)) {
          this.setState({item})
        }
      })*/
    }
    this.getUserAddress();
  }
  // 选择地址后更新用户收货地址
  componentDidShow() {
    this.getUserAddress();
  }
  handelRouter(url = '/pages/index/index') {
    getRuntime().navigateTo(url);
  }
  // 获取用户收货地址
  getUserAddress() {
    // 获取用户选择的地址, 然后获取用户默认地址
    let defaultAddress = getRuntime().getStorage('userSelectAddress') || null;
    // 如果用户没有选择地址，从缓存中获取用户设置的默认地址
    if (defaultAddress) {
      this.setState({
        defaultAddress
      });
    } else {
      // 没有缓存从服务器获取地址
      let { PointMallStore } = this.props.stores;
      PointMallStore.getUserDefaultAddress({
        callback: res => {
          defaultAddress = res || null;
          this.setState({
            defaultAddress
          });
        }
      });
    }
  }
  inputChange(e) {
    this.setState({ message: e.detail.value });
  }
  // 立即付款, 先下单再付款
  placeOrder() {
    let { item, defaultAddress, message } = this.state;
    if (!defaultAddress) {
      getRuntime().showToast('请添加收货地址~');
      return;
    }
    let { PointMallStore } = this.props.stores;
    let itemSpecId = item.specNoId;
    let qty = item.itemNum;
    let addressId = defaultAddress.id;
    PointMallStore.placeOrder({
      form: {
        itemSpecId,
        qty,
        message,
        addressId,
        orderType: 2
      },
      callback: res => {
        // 下单成功
        this.pay(res);
      }
    });
  }
  // 支付
  pay(orderInfo) {
    /* getRuntime().commonPay(orderInfo.id,5,false, () => {
       getRuntime().navigateTo('/pages/mall/pay_success?id='+orderInfo.id)
     })*/
    let { PointMallStore } = this.props.stores;
    PointMallStore.pay({
      form: {
        orderId: orderInfo.id,
        payType: 1,
        isPayPoints: false
      },
      callback: res => {
        // 支付
        //let payParams = JSON.parse(res.payParams)
        getRuntime().wxPay(res, () => {
          getRuntime().navigateTo('/pages/mall/pay_success?id=' + orderInfo.id);
        });
      }
    });
  }
  render() {
    let { defaultAddress, item } = this.state;
    let itemNum = item.itemNum;
    let itemFreight = item.freight * itemNum;
    let itemPrice = item.itemPrice;
    let totalMoney = itemPrice + itemFreight;
    let publicDonations = (totalMoney * (item.publicDonations / 100)).toFixed(2); // 公益捐款
    return <View style={_styleSheet["mall-order-confirm"]}>
        {defaultAddress && <View onClick={this.handelRouter.bind(this, '/pages/mall/address')} style={_styleSheet["order-address"]}>
           <View style={_styleSheet["has-address"]}>
              <Image src={address} style={_styleSheet["address-icon"]} />
              <View style={_styleSheet["info"]}>
                 <View style={_styleSheet["name"]}>{defaultAddress.receiver} {defaultAddress.receiverPhone}</View>
                 <View style={_styleSheet["address"]}>{defaultAddress.receiverAddress}</View>
              </View>
              <Image src={right_white} style={_styleSheet["drop-down-icon"]} />
           </View>
        </View>}
        {!defaultAddress && <View onClick={this.handelRouter.bind(this, '/pages/mall/address')} style={_styleSheet["order-address"]}>
           <View style={_styleSheet["none-address"]}>新增收货地址
              <Image src={right_white} style={_styleSheet["drop-down-icon"]} />
           </View>
        </View>}
        <View style={_styleSheet["order-spec"]}>
          <View style={_styleSheet["l"]}>
            <Image src={img_09} style={_styleSheet["img"]} />
            <View style={_styleSheet["info"]}>
              <View style={_styleSheet["list-title"]}>{item.name}</View>
              <View style={_styleSheet["list-info"]}>
                
                {item.itemSpec}
              </View>
            </View>
          </View>
          <View style={_styleSheet["r"]}>
            <View style={_styleSheet["list-price"]}>
              <Text style={_styleSheet["list-small"]}>￥</Text>{item.itemSignPrice}</View>
            <View style={[_styleSheet["list-info"], { textAlign: 'right' }]}>
              <Text>x{item.itemNum}</Text>
            </View>
          </View>
        </View>

        <View style={_styleSheet["select-spec"]}>
          <View style={[_styleSheet["title"], _styleSheet["color"]]}>小计</View>
          <View style={_styleSheet["drop-down-con"]}>
            <Text style={[_styleSheet["con"], _styleSheet["color"]]}><Text style={_styleSheet["list-small"]}>￥</Text>{itemPrice}</Text>
          </View>
        </View>

        <View style={_styleSheet["interval"]} />

        <View style={_styleSheet["select-spec"]}>
          <View style={_styleSheet["title"]}>运费</View>
          <View style={_styleSheet["drop-down-con"]}>
            {itemFreight > 0 && <Text style={_styleSheet["con"]}><Text style={_styleSheet["list-small"]}>￥</Text>{itemFreight}</Text>}
            {itemFreight <= 0 && <Text style={_styleSheet["con"]}>免费配送</Text>}
          </View>
        </View>
        <View style={_styleSheet["interval"]} />

        <View style={[_styleSheet["from-wrap"], { padding: 0 }]}>
          <View style={_styleSheet["item-group"]}>
            <Textarea name="message" placeholderClass="placeholder-class" placeholder="请填写备注信息(买家留言)~" style={[_styleSheet["from-textarea"], { padding: 0 }]} onInput={this.inputChange.bind(this)} />
          </View>
        </View>

        {!defaultAddress && <View style={_styleSheet["none-address-tip"]}>
           *请在上方添加收货地址
        </View>}

        <View style={_styleSheet["footer-block"]} />
        <View style={[_styleSheet["page-footer2"], _styleSheet["page-footer3"]]}>
          <View style={_styleSheet["all-price"]}>
            <View style={_styleSheet["line"]}>
              已选 <Text style={[_styleSheet["color"], _styleSheet["all-count"]]}>{itemNum}</Text> 件商品，合计：
              <Text style={_styleSheet["color"]}><Text style={_styleSheet["list-small"]}>￥</Text>{totalMoney}</Text>
            </View>
            <View style={[_styleSheet["line"], _styleSheet["gy"]]}>
              公益捐款￥{publicDonations}元
            </View>
          </View>
          <View onClick={this.placeOrder.bind(this)} style={_styleSheet["btn-submit"]}>立即付款</View>
        </View>
      </View>;
  }
};
Personnel = tslib_1.__decorate([inject("stores"), observer], Personnel);
export default Personnel;